import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base_kit/home/video/controller/video_detail_controller.dart';
import 'package:get/get.dart';
import 'package:video_player/video_player.dart';

import '../../utils/log_util.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/18.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单12-11  视频列表 子Item UI
///lib/home/video/video_details_widget.dart
class VideoDetailsWidget extends StatelessWidget {
  //当前Item对应的索引
  final int widgetIndex;

  const VideoDetailsWidget({Key? key, required this.widgetIndex})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    //注入依赖
    Get.put(VideoDetailController(tag: "$widgetIndex"), tag: "$widgetIndex");
    return GetBuilder(
      id: "$widgetIndex",
      tag: "$widgetIndex",
      builder: (VideoDetailController controller) {
        return SizedBox(
          width: double.infinity,
          child: Stack(
            children: [
              //第一层的视频
              buildVideoWidget(controller),
              //第二层的控制按钮
              buildControllerWidget(controller)
            ],
          ),
        );
      },
    );
  }

  ///代码清单12-11-1
  Widget buildControllerWidget(VideoDetailController controller) {
    if (controller.isPlay) {
      //如果正在播放
      return Container();
    }

    return Positioned.fill(
      child: Stack(
        children: [
          Positioned.fill(
              child: Image.asset(
            'assets/images/welcome_bg.jpeg',
            fit: BoxFit.fitWidth,
          )),
          Positioned.fill(
              child: Container(
            //0.3 的蓝色透明度
            color: Colors.blueGrey.withOpacity(0.5),
            //图标
            child: GestureDetector(
              onTap: () {
                //播放视频
                controller.play();
              },
              child: Icon(Icons.play_circle_fill, size: 44),
            ),
          ))
        ],
      ),
    );
  }

  ///代码清单12-11-2
  buildVideoWidget(VideoDetailController controller) {
    //获取视频播放控制器
    VideoPlayerController? playerController = controller.playController;
    if (playerController == null) {
      //读者也可以在此实现控制层
      return Text("点击播放");
    }
    return Positioned.fill(
      child: GestureDetector(
        onTap: () {
          //视频正在播放时 点击暂停
          Get.find<VideoDetailController>(tag: "$widgetIndex").pause();
        },
        child: AspectRatio(
          //视频比例
          aspectRatio: playerController.value.aspectRatio,
          child: VideoPlayer(playerController),
        ),
      ),
    );
  }
}
